package com.example.demo.controller;

import com.example.demo.dto.ModifyOrderRquest;
import com.example.demo.dto.OrderItemConfirmRequest;
import com.example.demo.dto.OrdersPageRequest;
import com.example.demo.dto.PlaceOrderRequest;
import com.example.demo.entity.Orders;
import com.example.demo.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * OrdersController
 *
 * @author liujin
 * @date 2020/12/18 13:59
 */
@Api(tags = "订单模块")
@RestController
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @ApiOperation(value = "下单")
    @PostMapping("/orders/placeOrder")
    public ResponseEntity placeOrder(@RequestBody @Valid PlaceOrderRequest request) {
        ordersService.placeOrder(request);
        return ResponseEntity.ok("下单成功");
    }

    @ApiOperation(value = "修改订单（加菜、退菜)")
    @PostMapping("/orders/modifyOrder")
    public ResponseEntity modifyOrder(@RequestBody @Valid ModifyOrderRquest request) {
        synchronized (request.getOrderSn()) {
            ordersService.modifyOrder(request);
        }
        return ResponseEntity.ok("修改订单成功");
    }

    @ApiOperation(value = "确认已完成上菜的菜品数量")
    @PostMapping("/orders/orderItemConfirm")
    public ResponseEntity orderItemConfirm(@RequestBody @Valid OrderItemConfirmRequest request) {
        ordersService.confirm(request);
        return ResponseEntity.ok("确认已完成上菜的菜品OK");
    }


    @ApiOperation(value = "查看订单")
    @PostMapping("/orders/page")
    public List<Orders> page(@RequestBody @Valid OrdersPageRequest request) {
        return ordersService.page(request);
    }


}
